﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Data.Maps;

namespace Data
{
    public class Map
    {
        public int x = 0;
        public int y = 0;
        public List<Block> listblock = new List<Block>();
        public Dictionary<int, int> listSpawn = new Dictionary<int, int>();

        public Map()
        {
            //liste des blocks de la map
            listblock.Add(new Block(240, 1125, 0, 0));
            listblock.Add(new Block(150, 375, 0, 1135));
            listblock.Add(new Block(32, 48, 150, 1135));
            listblock.Add(new Block(54, 200, 220, 930));
            listblock.Add(new Block(54, 54, 180, 1100));
            
            
            listSpawn.Add(233, -968);
            listSpawn.Add(-235, -712);
            listSpawn.Add(119, -624);
            listSpawn.Add(-589, -255);
            listSpawn.Add(-849, -605);
            listSpawn.Add(-1159, -527);
            listSpawn.Add(-1445, -273);
            listSpawn.Add(-965, 34);
            listSpawn.Add(-1133, -300);
        }

        public bool isAllowedPosition(int x, int y, int spriteX, int spriteY, int mapHeight, int mapWidth, float mapOriginX, float mapOriginY)
        {
            int positionPersoX = x - (int)mapOriginX;
            int positionPersoY = y - (int)mapOriginY;
            if (positionPersoY + spriteY < mapHeight
                    && positionPersoY > 0
                    && positionPersoX + spriteX < mapWidth
                    && positionPersoX > 0)
            {
                foreach(Block block in listblock)
                {
                    int positionBlockX = block.posX;
                    int positionBlockY = block.posY;
                    // gauche
                    // droite
                    // bas
                    // haut
                    if (positionPersoX + spriteX > positionBlockX
                        && positionPersoX < positionBlockX + block.width
                        && positionPersoY + spriteY > positionBlockY
                        && positionPersoY + (spriteY/2) < positionBlockY + block.height
                       )
                        return false;
                }
                
            }
            else
            {
                return false;
            }
            return true;
        }

        public void GetSpawnPosition(ref int x, ref int y, ref float mapX, ref float mapY, int frameWidth, int frameHeight)
        {
            //random get spawn
            KeyValuePair<int,int> key = listSpawn.ElementAt(new Random().Next(0,listSpawn.Count-1));

            //affectation posX et posY
            x = frameWidth / 2;
            y = frameHeight / 2;

            //Calcul position écran
            mapX = key.Key ;
            mapY = key.Value ;
        }

        
    }

}
